## Obj  

使用
```
const _objectmethods = require("andy-aui/lib/object");
```

### deepClone [> 1.9.12]
Creates a deep clone of an object.
```javascript
const deepClone = obj => {
  let clone = Object.assign({}, obj);
  Object.keys(clone).forEach(
    key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
  );
  return clone;
};
```
example
```javascript
const a = { foo: 'bar', obj: { a: 1, b: 2 } };
const b = deepClone(a); // a !== b, a.obj !== b.obj
```

### defaults [> 1.9.12]
Assigns default values for all properties in an object that are undefined.
```javascript
const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
```
example
```javascript
defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
```

### equals [> 1.9.12]
Performs a deep comparison between two values to determine if they are equivalent.
```javascript
const equals = (a, b) => {
  if (a === b) return true;
  if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
  if (!a || !b || (typeof a != 'object' && typeof b !== 'object')) return a === b;
  if (a === null || a === undefined || b === null || b === undefined) return false;
  if (a.prototype !== b.prototype) return false;
  let keys = Object.keys(a);
  if (keys.length !== Object.keys(b).length) return false;
  return keys.every(k => equals(a[k], b[k]));
};
```
example
```javascript
equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' }); // true
```
 
### findKey [> 1.9.12]
Returns the first key that satisfies the provided testing function. Otherwise undefined is returned.
```javascript
const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj));
```
example
```javascript
findKey(
  {
    barney: { age: 36, active: true },
    fred: { age: 40, active: false },
    pebbles: { age: 1, active: true }
  },
  o => o['active']
); // 'barney'
```

### findLastKey [> 1.9.12]
Returns the last key that satisfies the provided testing function. Otherwise undefined is returned.
```javascript
const findLastKey = (obj, fn) =>
  Object.keys(obj)
    .reverse()
    .find(key => fn(obj[key], key, obj));
```
example
```javascript
findLastKey(
  {
    barney: { age: 36, active: true },
    fred: { age: 40, active: false },
    pebbles: { age: 1, active: true }
  },
  o => o['active']
); // 'pebbles'
```

### flattenObject [> 1.9.1]
Flatten an object with the paths for keys.
```javascript
const flattenObject = (obj, prefix = '') =>
  Object.keys(obj).reduce((acc, k) => {
    const pre = prefix.length ? prefix + '.' : '';
    if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
    else acc[pre + k] = obj[k];
    return acc;
  }, {});
```
example
```javascript
flattenObject({ a: { b: { c: 1 } }, d: 1 }); // { 'a.b.c': 1, d: 1 }
```

### get [> 1.2.0]
Retrieve a set of properties indicated by the given selectors from an object.
```javascript
const get = (from, ...selectors) =>
  [...selectors].map(s =>
    s
      .replace(/\[([^\[\]]*)\]/g, '.$1.')
      .split('.')
      .filter(t => t !== '')
      .reduce((prev, cur) => prev && prev[cur], from)
  );
```
example 
```javascript
const obj = { selector: { to: { val: 'val to select' } }, target: [1, 2, { a: 'test' }] };
get(obj, 'selector.to.val', 'target[0]', 'target[2].a'); // ['val to select', 1, 'test']
```

### mapKeys [> 1.9.12]
Creates an object with keys generated by running the provided function for each key and the same values as the provided object.
```javascript
const mapKeys = (obj, fn) =>
  Object.keys(obj).reduce((acc, k) => {
    acc[fn(obj[k], k, obj)] = obj[k];
    return acc;
  }, {});
```
example
```javascript
mapKeys({ a: 1, b: 2 }, (val, key) => key + val); // { a1: 1, b2: 2 }
```

### matchesWith [> 1.9.12]
Compares two objects to determine if the first one contains equivalent property values to the second one, based on a provided function.
```javascript
const matchesWith = (obj, source, fn) =>
  Object.keys(source).every(
    key =>
      obj.hasOwnProperty(key) && fn
        ? fn(obj[key], source[key], key, obj, source)
        : obj[key] == source[key]
  );
```
example
```javascript
const isGreeting = val => /^h(?:i|ello)$/.test(val);
matchesWith(
  { greeting: 'hello' },
  { greeting: 'hi' },
  (oV, sV) => isGreeting(oV) && isGreeting(sV)
); // true
```

### objectFromPairs [> 1.9.12]
Creates an object from the given key-value pairs.
```javascript
const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
```
example
```javascript
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}
```

### objectToPairs [> 1.9.12]
Creates an array of key-value pair arrays from an object.
```javascript
const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
```
example
```javascript
objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]]
```

### omitByKeys [> 1.9.12]
Omits the key-value pairs corresponding to the given keys from an object.
```javascript
const omitByKeys = (obj, arr) =>
  Object.keys(obj)
    .filter(k => !arr.includes(k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
```
example
```javascript
omitByKeys({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }  
```

### omitBy [> 1.9.12]
Creates an object composed of the properties the given function returns falsey for. The function is invoked with two arguments: (value, key).
```javascript
const omitBy = (obj, fn) =>
  Object.keys(obj)
    .filter(k => !fn(obj[k], k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
```
example
```javascript
omitBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { b: '2' }
```

### orderBy [> 1.9.12]
Returns a sorted array of objects ordered by properties and orders.
```javascript
const orderBy = (arr, props, orders) =>
  [...arr].sort((a, b) =>
    props.reduce((acc, prop, i) => {
      if (acc === 0) {
        const [p1, p2] = orders && orders[i] === 'desc' ? [b[prop], a[prop]] : [a[prop], b[prop]];
        acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
      }
      return acc;
    }, 0)
  );
```
example
```javascript
const users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }];
orderBy(users, ['name', 'age'], ['asc', 'desc']); // [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
```

### pickByKeys [> 1.9.12]
Picks the key-value pairs corresponding to the given keys from an object
```javascript
const pickByKeys = (obj, arr) =>
  arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
```
example
```javascript
pickByKeys({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
```        

### pickBy [> 1.9.12]
Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key).
```javascript
const pickBy = (obj, fn) =>
  Object.keys(obj)
    .filter(k => fn(obj[k], k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
```
example
```javascript
pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { 'a': 1, 'c': 3 }
```

### unflattenObject [> 1.9.12]
Unlatten an object with the paths for keys.
```javascript
const unflattenObject = obj =>
  Object.keys(obj).reduce((acc, k) => {
    if (k.indexOf('.') !== -1) {
      const keys = k.split('.');
      Object.assign(
        acc,
        JSON.parse(
          '{' +
            keys.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`)).join('') +
            obj[k] +
            '}'.repeat(keys.length)
        )
      );
    } else acc[k] = obj[k];
    return acc;
  }, {});
```
example
```javascript
unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 }
```